package com.test.ServletUpload;

import com.test.BRBInference.BRBInference;
import com.test.jdbc.JDBCUtils2;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "Table_Drop",value = "/Table_Drop")
public class Table_Drop extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        if (ServletFileUpload.isMultipartContent(request)) { //请求表单的格式
            FileItemFactory fileItemFactory = new DiskFileItemFactory();  //缓存区
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

            try {
                List<FileItem> list = servletFileUpload.parseRequest(request);  //表单列表
                for (FileItem fileItem : list) {

                    if (fileItem.isFormField()) { //普通表
                        String fileName = fileItem.getString();
                        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
                        try {
                            queryRunner.update("drop table "+fileName + ";");
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }

            } catch (FileUploadException e) {
                e.printStackTrace();
            }

            request.getSession().setAttribute("Drop_flag", "finished");

            response.sendRedirect("/Java_web/Drop_table.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
